package com.bj58.mis.apm.server.core.common.result;

import com.bj58.mis.apm.server.core.common.consts.ResultCodeConsts;
import com.bj58.mis.apm.server.core.common.exception.BizException;

import java.io.Serializable;



public class ResultBean<T> implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public static final int CODE_SUCCESS = ResultCodeConsts.SUCCESS;
	
	public static final int CODE_FAIL = ResultCodeConsts.UNKONW_ERROR;
	
	public static final String DEFUALT_SUCCESS_MSG = "ok";
	
	private int code;
	private String message;
	private T data;
	
	public  ResultBean(int code, String message, T data){
		this.code = code;
		this.message = message;
		this.data = data;
	}
	
	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}
	

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@SuppressWarnings("unchecked")
	public  T getData() {
		return (T) data;
	}

	public void setData(T data) {
		this.data = data;
	}
	
	public static <T> ResultBean<T> success(T data){
		return success(data, DEFUALT_SUCCESS_MSG);
	}

	public static <T> ResultBean<T> success(T data, String message){
		return new ResultBean<T>(ResultBean.CODE_SUCCESS, message, data);
	}
	public static ResultBean error(Throwable e){
		if(e instanceof BizException){
			BizException bizExp = (BizException) e;
			return new ResultBean(bizExp.getCode(), bizExp.getMessage(), null);
		}
		return new ResultBean(ResultBean.CODE_FAIL, e.getMessage(), null);
	}
	public static ResultBean error(String message){
		return new ResultBean(ResultBean.CODE_FAIL, message, null);
	}
	public static ResultBean error(int code, String message){
		return new ResultBean(code, message, null);
	}
	
	
	public static void main(String[] args) {
		error(new RuntimeException("xxxxx"));
	}
}
